{{ $current_page := . }}

{{/* Check whether to show breadcrumb navigation. */}}
{{ $show_breadcrumb := .Params.show_breadcrumb }}

<div class="container-fluid docs">
  <div class="row flex-xl-nowrap">
    <div class="col-12 col-md-3 col-xl-2 docs-sidebar">
      {{ partial "book_sidebar" . }}
    </div>

    {{/* Show ToC by default. */}}
    {{ if ne .Params.toc false }}
    <div class="d-none d-xl-block col-xl-3 docs-toc">
      {{ partial "functions/get_hook" (dict "hook" "toc-start" "context" .) }}

      <ul class="nav toc-top">
        <li><a href="#" id="back_to_top" class="docs-toc-title">{{ i18n "on_this_page" }}</a></li>
      </ul>

      {{ .TableOfContents }}

      {{ partial "functions/get_hook" (dict "hook" "toc-end" "context" .) }}
    </div>
    {{ end }}

    <main class="col-12 col-md-9 col-xl-7 py-md-3 pl-md-5 docs-content" role="main">

      <article class="article">

        <div class="docs-article-container">
          {{ if $show_breadcrumb }}
            {{ partial "components/breadcrumb" $current_page }}
          {{ end }}
        </div>

        {{/* Cover image */}}
        {{ if and .Params.cover.image }}
        <div class="article-header">
          {{- $img := "" -}}
          {{ $alt := (.Params.cover.caption | default "") | plainify }}
          {{ $destination := .Params.cover.image }}
          {{ $is_remote := strings.HasPrefix $destination "http" }}
          {{- if not $is_remote -}}
            {{- $img = (.Page.Resources.ByType "image").GetMatch $destination -}}
            {{- if not $img -}}
              {{- $img = resources.Get (path.Join "media" $destination) -}}
            {{- end -}}
          {{- end -}}
          {{ if $img }}
            {{ $img = $img.Fit "1800x310 webp" }}
            <img src="{{$img.RelPermalink}}" width="{{$img.Width}}" height="{{$img.Height}}" class="article-banner" alt="{{$alt}}">
          {{ else }}
            <img src="{{ .Params.cover.image }}" class="article-banner" alt="{{$alt}}">
          {{ end }}

          {{ with .Params.cover.caption }}<span class="article-header-caption">{{ . | markdownify | emojify }}</span>{{ end }}
        </div>
        {{end}}

        <div class="docs-article-container">
          {{/* Fallback to filename as title (useful for default Obsidian note structure) */}}
          <h1>{{ cond (ne .File nil) (.Title | default (title .File.ContentBaseName)) .Title }}</h1>

          <div class="article-style">

            {{/* Only auto-list page children if _index has no content (or if it isn't a .File?) */}}
            {{ if and (not .Content) (gt (len $.Page.Pages) 0) }}
            <ul class="list-unstyled">
              {{ range $.Page.Pages }}
              <li>
                <h5><a href="{{.RelPermalink}}">{{.LinkTitle | default (title .File.TranslationBaseName) }}</a></h5>
                {{if .Summary}}<p>{{.Summary | plainify | emojify}}</p>{{end}}
              </li>
              {{end}}
            </ul>
            {{end}}

            {{ .Content }}

          </div>

          {{ partial "tags.html" . }}

          {{/* Show next/previous pages by default. */}}
          {{ if ne .Params.pager false }}
          <div class="article-widget">
            {{ partial "components/pager" . }}
          </div>
          {{ end }}
        </div>

        <div class="body-footer">
          <p>{{ i18n "last_updated" }} {{ $.Lastmod | time.Format site.Params.locale.date_format }}</p>

          {{ partial "page_edit" . }}

          {{ partial "comments" . }}

          {{ partial "page_related" . }}
        </div>

      </article>

      {{ partial "site_footer" . }}

    </main>
  </div>
</div>
